package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.GraduateProjectDao;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProject;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectStatus;
import cn.edu.sdjzu.xg.bysj.domain.Teacher;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.function.BinaryOperator;

public final class GraduateProjectService {
	//获取 GraduateProjectDao实例
	private GraduateProjectDao graduateProjectDao= GraduateProjectDao.getInstance();
	//新建 GraduateProjectService对象
	private static GraduateProjectService graduateProjectService = new GraduateProjectService();
	//构造器
	private GraduateProjectService(){}
	//获取GraduateProjectService对象
	public static GraduateProjectService getInstance(){
		return graduateProjectService;
	}
	/**
	 * 根据id查找GraduateProject信息
	 *
	 * @param id
	 *
	 * @return
	 * @throws SQLException
	 */
	public GraduateProject find(int id) throws SQLException {
		//获取数据库链接
		Connection connection = JdbcHelper.getConn();
		GraduateProject graduateProject =null;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			graduateProject = graduateProjectDao.find(connection,id);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return graduateProject;
	}
	/**
	 * 分页查询GraduateProject信息
	 *
	 * @param condition
	 * @param pagination
	 *
	 * @return
	 * @throws SQLException
	 */
	public Collection<GraduateProject> findAll(Collection<Condition> condition, Pagination pagination) throws SQLException {
		Connection connection =  JdbcHelper.getConn();
		Collection<GraduateProject> graduateProjects =null;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			graduateProjects = graduateProjectDao.findAll(connection,condition,pagination);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return  graduateProjects;
	}

	/**
	 * 新增GraduateProject
	 *
	 * @param graduateProject
	 *
	 * @return
	 * @throws SQLException
	 */
	public boolean add(GraduateProject graduateProject) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		boolean addded = false;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			addded = graduateProjectDao.add(connection,graduateProject);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return addded;
	}
	/**
	 * 根据id删除GraduateProject
	 *
	 * @param id
	 *
	 * @return
	 * @throws SQLException
	 */
	public boolean delete(int id) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		boolean deleted = false;
		deleted = graduateProjectDao.delete(connection,id);
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			deleted = graduateProjectDao.delete(connection,id);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return deleted;
	}
	/**
	 * 修改GraduateProject
	 *
	 * @param graduateProject
	 *
	 * @return
	 * @throws SQLException
	 */
	public boolean update(GraduateProject graduateProject) throws SQLException {
		//获取数据库链接
		Connection connection = JdbcHelper.getConn();
		boolean updated = false;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			updated = graduateProjectDao.update(connection,graduateProject);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return updated;
	}
}
